<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据类型" prop="type">
        <el-select v-model="queryParams.type" clearable placeholder="请选择状态"  >
          <el-option
            v-for="dict in dict.type.out_stock_order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择状态"  >
          <el-option
            v-for="dict in dict.type.picker_task_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="来源单号" prop="relOrderNo">
        <el-input
          v-model="queryParams.relOrderNo"
          placeholder="请输入来源单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拣货员" prop="pickerName">
        <el-input
          v-model="queryParams.pickerName"
          placeholder="请输入拣货员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-button
        type="success"
        plain
        icon="el-icon-view"
        size="mini"
        :disabled="single"
        @click.native="getPickingRecords"
        v-hasPermi="['inOutStock:pickerTaskOrder:getPickingRecords']"
      >拣货记录</el-button>
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inOutStock:pickerTaskOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['inOutStock:pickerTaskOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['inOutStock:pickerTaskOrder:remove']"
        >删除</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="table" :height="calcTableHeight" v-loading="loading" :data="orderList" @selection-change="handleSelectionChange" highlight-current-row @current-change="rowSelection">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="no" width="150" show-overflow-tooltip />
      <el-table-column label="单据类型" align="center" prop="type" width="80" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.out_stock_order_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.picker_task_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="物料编号" align="center" prop="productNo" width="120" show-overflow-tooltip />
      <el-table-column label="物料名称" align="center" prop="productName" width="120" show-overflow-tooltip />
      <el-table-column label="来源单号" align="center" prop="relOrderNo" width="150" show-overflow-tooltip />
      <el-table-column label="拣货员" align="center" prop="pickerName" width="100" show-overflow-tooltip />
      <el-table-column align="center" prop="storehouseNo" :label="$t('inOutStock.outStockOrder.storehouseNo')" width="120" show-overflow-tooltip />
      <el-table-column label="单据数量" align="center" prop="orderQty" width="80" show-overflow-tooltip />
      <el-table-column align="center" prop="distributeQty" :label="$t('inOutStock.outStockOrder.distributeQty')" width="120" show-overflow-tooltip />
      <el-table-column label="拣货数量" align="center" prop="pickingQty" width="80" show-overflow-tooltip />
      <el-table-column label="完成时间" align="center" prop="completeTime" width="155" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="qualityTaskNo" :label="$t('inOutStock.outStockOrder.qualityTaskNo')" width="120" show-overflow-tooltip />
      <el-table-column label="创建人" align="center" prop="createByName" width="80" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="155" show-overflow-tooltip />
      <el-table-column label="最后更新人" align="center" prop="updateByName" width="100" show-overflow-tooltip />
      <el-table-column label="最后更新时间" align="center" prop="updateTime" width="155" show-overflow-tooltip />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 右边抽屉页 -->
    <el-drawer
      title="拣货记录"
      :visible.sync="drawerShow"
      direction="rtl"
      :before-close="handleClose"
      size="40%">
      <el-table :data="pickingRecords">
        <el-table-column align="center" property="barcode" label="条码" />
        <el-table-column align="center" property="pickingQty" label="拣货数" />
        <el-table-column align="center" property="storehouseLocationNo" label="库位编号"/>
      </el-table>
    </el-drawer>

    <!-- 添加或修改拣货任务单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务单号" prop="no">
          <el-input v-model="form.no" placeholder="请输入任务单号" />
        </el-form-item>
        <el-form-item label="拣货员" prop="picker">
          <el-input v-model="form.picker" placeholder="请输入拣货员" />
        </el-form-item>
        <el-form-item label="物料编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入物料编号" />
        </el-form-item>
        <el-form-item label="关联编号" prop="relOrderNo">
          <el-input v-model="form.relOrderNo" placeholder="请输入关联编号" />
        </el-form-item>
        <el-form-item label="单据数量" prop="orderQty">
          <el-input v-model="form.orderQty" placeholder="请输入单据数量" />
        </el-form-item>
        <el-form-item label="拣货数量" prop="pickingQty">
          <el-input v-model="form.pickingQty" placeholder="请输入拣货数量" />
        </el-form-item>
        <el-form-item label="完成时间" prop="completeTime">
          <el-date-picker clearable
            v-model="form.completeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否删除：1是 0否" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除：1是 0否" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, pickingRecordList } from "@/api/inOutStock/pickerTaskOrder";

export default {
  name: "Order",
  dicts:['out_stock_order_type','picker_task_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      nos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拣货任务单表格数据
      orderList: [],
      // 拣货单记录列表
      pickingRecords: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        type: null,
        status: null,
        picker: null,
        pickerName: null,
        productNo: null,
        relOrderNo: null,
        orderQty: null,
        pickingQty: null,
        completeTime: null,
        sort: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        no: [
          { required: true, message: "任务单号不能为空", trigger: "blur" }
        ],
        picker: [
          { required: true, message: "拣货员不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "是否删除：1是 0否不能为空", trigger: "blur" }
        ],
      },
      //抽屉页是否展示
      drawerShow: false,
      //列表行选择的数据
      orderData:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询拣货任务单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        type: null,
        status: 0,
        picker: null,
        productNo: null,
        relOrderNo: null,
        orderQty: null,
        pickingQty: null,
        completeTime: null,
        delFlag: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null,
        sort: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.nos = selection.map(item => item.no);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拣货任务单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改拣货任务单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const nos = row.no || this.nos;
      this.$modal.confirm(this.$t('deleteTip', { num: ids.length })).then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('inOutStock/pickerTaskOrder/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    /** 根据拣货单编号获取拣货记录 */
    getPickingRecords(){
      let row = null;
      this.orderList.forEach((item) => {
        if(item.id == this.ids[0]){
          row = item;
        }
      });

      pickingRecordList({orderDetailNo: row.no,pageSize:0}).then(response => {
        this.pickingRecords = response.rows;
        this.drawerShow = true;
      });
    },
    // 抽屉页面关闭
    handleClose(done) {
      done();
      // this.$confirm('确认关闭？')
      //   .then(_ => {
      //     done();
      //   })
      //   .catch(_ => {});
    }
  }
};
</script>
